import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class UdpEchoClient {
        private  DatagramSocket socket=null;
        private String serviceIP;
        private int servicePort;
    public UdpEchoClient(String serviceIP, int servicePort) throws IOException{
        socket=new DatagramSocket();
        this.serviceIP=serviceIP;
        this.servicePort=servicePort;
    }

    public void start() throws IOException {
        System.out.println("启动客户端");
        Scanner scanner=new Scanner(System.in);
        while (true){
            System.out.println("->");
            String request=scanner.next();
            DatagramPacket requestPacket=new DatagramPacket(request.getBytes(),request.getBytes().length,
                    InetAddress.getByName(this.serviceIP), this.servicePort);
            socket.send(requestPacket);
            DatagramPacket responsePacket=new DatagramPacket(new byte[4096],4096);
            socket.receive(responsePacket);
            String respon=new String(responsePacket.getData(),0,responsePacket.getLength());
        }
    }
    public static void main(String[] args) throws IOException {
        UdpEchoClient client=new UdpEchoClient("127.0.0.1",9090);
        client.start();
    }
}
